package com.xpgk.mvc.index;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.btoShop.BtoShop;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.right.Right;
import com.xpgk.mvc.user.User;

@Service(name = IndexService.serviceName)
public class IndexService extends BaseService {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(IndexService.class);

	public static final String serviceName = "indexService";

	/**
	 * 查询用户可操作的菜单
	 * 
	 * @param systemsIds
	 * @param user
	 * @param i18n
	 * @return
	 */
	public List<Right> menu(User user) {
		String stationIds = user.getStr(User.column_stationids);

		String oneSql = getSqlMy(Right.sqlId_one_menu);
		String twoSql = getSqlMy(Right.sqlId_two_menu);

		// 一级菜单
		List<Right> oneList = Right.dao.find(oneSql, stationIds);
		for (Right oneMenu : oneList) {
			// 二级菜单
			String pMenuIds = oneMenu.getPKValue();
			List<Right> twoList = Right.dao.find(twoSql, stationIds, pMenuIds);
			oneMenu.put("subList", twoList);
		}

		return oneList;
	}
	
	/**
	 * 新增人数
	 * @param organ
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public Long getNewCustomerNum(String organIds, String registtime){
		//String sql = getSqlMy(IndexSQL.sqlId_newCustomerNum);
		Long num = 0l;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("registtime", registtime);
		param.put("organIds", organIds);
		List<Record> lis = find(IndexSQL.sqlId_newCustomerNum, param);
		if(lis!=null&&lis.size()>0){
			 num =lis.get(0).getLong("num");
		}
		//Long num = Db.findFirst(sql).getLong("num");
		return num;
	}
	
	/**
	 * 减重销售金额
	 * @return
	 */
	public Double getDelWeightSales(Organization organ, String startTime){
		String sql = getSqlMy(IndexSQL.sqlId_delWeightSales);
		Double prices = Db.findFirst(sql, startTime,  Code.lc_ids, organ.getStr(Organization.column_bh)).getDouble("prices");
		return prices;
	}
	
	/**
	 * 产品销售金额
	 * @param organ
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public Double getProductSales(Organization organ, String startTime, String endTime){
		String sql = getSqlMy(IndexSQL.sqlId_productSales);
		Double prices = Db.findFirst(sql, startTime, endTime, Code.xflb_gm, Code.lc_ids, organ.getStr(Organization.column_bh)).getDouble("prices");
		return prices;
	}
	
	/**
	 * 减重销售明细
	 * @return
	 * @throws ParseException 
	 */
	public List<Double> getDelWeightSalesDetail(Organization organ, List<String> days){
		List<Double> result = new ArrayList<>();
		for(String day : days){
			String sql = getSqlMy(IndexSQL.sqlId_delWeightSalesByDay);
			Double prices = Db.findFirst(sql, day, Code.lc_ids, organ.getStr(Organization.column_bh)).getDouble("prices");
			if(null == prices){
				result.add(0.0);
			}else{
				result.add(prices);
			}
		}
		return result;
	}
	
	/**
	 * 产品销售明细
	 * @param organ
	 * @param days
	 * @return
	 */
	public List<Double> getProductSalesDetail(Organization organ, List<String> days){
		List<Double> result = new ArrayList<>();
		for(String day : days){
			String sql = getSqlMy(IndexSQL.sqlId_productSalesByDay);
			Double prices = Db.findFirst(sql, day, Code.lc_ids, Code.xflb_gm, organ.getStr(Organization.column_bh)).getDouble("prices");
			if(null == prices){
				result.add(0.0);
			}else{
				result.add(prices);
			}
		}
		return result;
	}
	/**预警
	 * 
	 * @param organIds
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws ParseException
	 */
	public Map<String,Integer>  getWarning(String organIds,String startTime,String endTime) throws ParseException{	
		Map<String,Integer>  warMap=new HashMap<>();
		int zcCus=0;//涨称顾客
		int pcCus=0;//平称顾客
		int zdCus=0;//转店顾客
		int wddCus=0;//未到店顾客
		int dlcbzCus=0;//单疗程不足顾客
		//到店服务顾客
		/*String  sql  =getSqlMy(CustomerService.sqlId_selByOrganDate);
		List<CustomerService>  csList=CustomerService.dao.find(sql,organIds,startTime,endTime);
		if(csList!=null &&csList.size()>0)
		{
			for(CustomerService css:csList)
			{	
				if(css!=null)
				{
					String cussql=getSqlMy(CustomerService.sqlId_selByDate);
					List<CustomerService>  cusList=CustomerService.dao.find(cussql,css.getStr(CustomerService.column_customerID),organIds,startTime,endTime);
					CustomerService  cs=cusList.get(cusList.size()-1);
					if(cs.getFloat(CustomerService.column_weight)>cs.getFloat(CustomerService.column_lastWeight))
					{
						zcCus++;
					}
					if(cs.getFloat(CustomerService.column_weight)==cs.getFloat(CustomerService.column_lastWeight))
					{
						pcCus++;
					}
				}
			}
		}*/
		
		
		warMap.put("zcCus", zcCus);
		warMap.put("pcCus", pcCus);
		
		//转店顾客
		String  zdCusSql=getSqlMy(BtoShop.sqlId_getOutShopCus);
		List<BtoShop>  bsList=BtoShop.dao.find(zdCusSql,organIds,startTime,endTime);
		zdCus=bsList.size();
		warMap.put("zdCus", zdCus);
		
		//不到店顾客
		String  wddcussql=getSqlMy(CustomerService.sqlId_selNotoStoreByOrganDate);
		List<CustomerService>  wddcusList=CustomerService.dao.find(wddcussql,organIds,startTime,endTime);
		wddCus=wddcusList.size();
		warMap.put("wddCus", wddCus);
		
		//单疗程不足顾客
		String  dlcprosql=getSqlMy(Product.sqlId_ylc);//通过类别，产品单位，产品容量获取单疗程
		List<Product>  dlcproList=Product.dao.find(dlcprosql,Code.lc_ids,Code.dlc_units,Code.dlc_capacity);
		if(dlcproList!=null && dlcproList.size()>0)
		{
			for(Product  pro:dlcproList)
			{
				List<CustomerProduct>  cpList=CustomerProduct.dao.getCusProByope(organIds, pro.getStr(Product.column_ids), Code.dlc_warring);
				dlcbzCus+=cpList.size();
			}
		}
		warMap.put("dlcbzCus", dlcbzCus);
		
		return warMap;
	}
	
	/**获取各疗程销售金额
	 * 
	 * @param organIds
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public  List<Record>  getTreatmentRatio(String organIds,String startTime,String endTime)
	{
		List<Record>  result=new  ArrayList<>();
		Organization  organ=Organization.dao.findById(organIds);
		String  lczbsql=getSqlMy(IndexSQL.sqlId_everyLcProSalesNum);
		List<ProductSales>  psaList=ProductSales.dao.find(lczbsql,startTime,endTime,organ.getStr(Organization.column_bh),Code.lc_ids);
		if(psaList!=null && psaList.size()>0)
		{
			for(int i=0;i<psaList.size();i++)
			{	
				Record  record=new Record();
				record.set("value", psaList.get(i).getDouble("totalPrice"));
				if(psaList.get(i).getInt("number")==1)
				{
					record.set("name", psaList.get(i).getStr("proname"));
				}else
				{
					record.set("name", psaList.get(i).getInt("number")+"疗程");
				}
				result.add(record);
			}
		}
		
		
		return result;
	}
	
	
	/**获取各产品销售额
	 * 
	 * @param organIds
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public  List<Record> getProductRatio(String organIds,String startTime,String endTime)
	{
		List<Record>  result=new  ArrayList<>();
		Organization  organ=Organization.dao.findById(organIds);
		String  prosql=getSqlMy(IndexSQL.sqlId_everyProductSalesNum);
		List<ProductSales>  psaList=ProductSales.dao.find(prosql,organ.getStr(Organization.column_bh),startTime,endTime,Code.lc_ids);
		if(psaList!=null && psaList.size()>0)
		{
			for(int i=0;i<psaList.size();i++)
			{
				Record  record=new Record();
				record.set("name", psaList.get(i).getStr("proname"));
				record.set("value", psaList.get(i).getDouble("totalPrice"));
				result.add(record);
			}
		}
		return result;
		
	}

	public Double getDelWeight(String curOrganidS, String format) {
		Record  f = Db.findFirst("select SUM(gjWeight)   gjWeight  from  sb_customer_service_nurse  where   organids  = ?   and  YearMon  = ?",curOrganidS,format);
		if(f!=null&&f.getDouble("gjWeight")!=null){
			return f.getDouble("gjWeight");
		}
		return 0d;
	}
}
